<template>
    <div>
        <div class="pd-10">
            <el-table :data="tableData" border stripe :header-cell-class-name="headerBg">
                <el-table-column type="index" width="50" />
                <el-table-column prop="title" label="题目" width="400" />
                <el-table-column prop="" label="题解" width="110">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" @click="viewAnswer(scope.row)" round>显示题解</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="topicTypeId" label="类型" />
                <el-table-column prop="complexity" label="难度系数" width="100" />
                <el-table-column prop="hits" label="展示量" />
                <el-table-column prop="praiseCount" label="点赞数" />
                <el-table-column prop="commentCount" label="评论数" width="70" />
                <el-table-column prop="createTime" label="创建时间" width="150" />
                <el-table-column width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" circle @click="editTopic(scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle
                            @click="deleteTopticType(scope.row.id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 20, 30]" :current-page="pageNum" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="operation" :visible.sync="dialogFormVisible" width="20%">
            <el-form :model="form" size="small">
                <el-form-item label="语言名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>

                <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog :title="operation" :visible.sync="editFormVisible" width="200">
            <el-form :model="form" size="small">
                <el-form-item :label="designation" :label-width="formLabelWidth">
                    <WangEditor ref="childMethod" :title="form.title">
                    </WangEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="operation" :visible.sync="editAnswerVisible" width="200">
            <el-form :model="form" size="small">
                <el-form-item :label="designation" :label-width="formLabelWidth">
                    <WangEditor ref="childMethod" :title="form.answerTitle">
                    </WangEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editAnswerVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAnswer()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog :title="operation" customClass="customClass" :visible.sync="dialogTableVisible">
            <p style="font-weight: bold;">{{ topicTitle }}</p>
            <el-table :data="answerData">
                <el-table-column property="answerTitle" label="回答" width="700" />
                <el-table-column property="commentCount" label="获赞数" width="" />
                <el-table-column property="praiseCount" label="评论数" />
                <el-table-column property="commentCount" label="修改" width="">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" @click="editAnswer(scope.row)" circle></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>
<script>

import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import WangEditor from '@/components/wangEditor/WangEditor';
export default {
    components: { Editor, Toolbar, WangEditor },
    name: "TopicType",
    data() {
        return {
            tableData: [],
            pageSize: 10,
            pageNum: 1,
            total: 0,
            headerBg: "headerBg",
            formLabelWidth: "90px",
            dialogFormVisible: false,
            editFormVisible: false,
            dialogTableVisible: false,
            editAnswerVisible: false,
            topicTitle: "",
            operation: "",
            form: {},
            answerData: [],
            editor: null,
            childTitle: "",
            designation: "",
        }
    },
    created() {
        this.load();
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
        editTopic(row) {
            this.operation = "编辑题目";
            this.designation = "题目";
            this.form = row;
            this.editFormVisible = true;
        },
        editAnswer(row) {
            this.operation = "题解";
            this.designation = "答案";
            this.form = row;
            this.editAnswerVisible = true;
        },
        viewAnswer(row) {
            this.dialogTableVisible = true;
            this.topicTitle = row.title;
            this.request.get("/topic_java_answer/list/" + row.id).then(res => {
                if (res.code == 200) {
                    this.answerData = res.data;

                }
            })
        },
        TopicAdd() {
            this.$router.push("/topic_add")

        },
        getChildTitle() {
            return this.childTitle = this.$refs.childMethod.childTitle.slice(3, -4)
        },
        saveAnswer() {
            this.form.answerTitle = this.getChildTitle().replace(/<[^>]+>/g, "");
            this.request.post("/topic_java_answer/save", this.form).then(res => {
                if (res.code == 200) {
                    if (this.form.id.length == 0) {
                        this.$message.success("新增成功");
                        this.editAnswerVisible = false;
                        this.load();
                    } else {
                        this.$message.success("修改成功");
                        this.editAnswerVisible = false;
                        this.load();
                    }
                }
            })
        },
        save() {
            this.form.title = this.getChildTitle();
            this.request.post("/topic_java/save", this.form).then(res => {
                if (res.code == 200) {
                    if (this.form.id.length == 0) {
                        this.$message.success("新增成功");
                        this.dialogFormVisible = false;
                        this.load();
                    } else {
                        this.$message.success("修改成功");
                        this.editFormVisible = false;
                        this.load();
                    }
                }
                else {
                    this.$message.error(res.msg);
                }
            });
        },
        load() {
            this.request.get("/topic_java/listPage", {
                params: {
                    pageNumber: this.pageNum,
                    pageSize: this.pageSize,
                }
            }).then(res => {
                this.tableData = res.data.list;
                this.total = res.data.total;
            });
        },
        deleteTopticType(id) {
            this.$confirm("此操作将永久删除该, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.request.delete("/topic_java/delete/" + id).then(res => {
                    if (res.code == 200) {
                        this.$message.success("删除成功");
                        this.load();
                    }
                    else {
                        this.$message.error(res.msg);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消删除"
                });
                this.load();
            });
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {
            this.pageNum = pageNum;
            this.load();
        },
    },
    watch: {
        dialogFormVisible: {
            handler() {
                this.form = {
                    id: "",
                    name: "",
                    remark: '',
                    hits: "",
                    count: "",
                };
                this.load();
            },

        },
        editFormVisible: {
            handler() {
                this.load();
            }
        },
    },
    mounted() {
        setTimeout(() => {
            this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
        }, 1500)
    },
    beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
    }
}
</script>
<style>
.headerBg {
    background: #eee !important;
}

.customClass {
    width: 67% !important;
    min-width: 100px !important;
}
</style>